public class Shop extends GameStage {
  boolean refresh;
  boolean backtomap = false;
  ArrayList<Button> buylist2;
  ArrayList<InvSlot> buylist = new ArrayList<InvSlot>();
  ArrayList<Object> r = new ArrayList<Object>();
  Scanner sc = null;
  ArrayList<Button> selllist;
  Player buyer = null;
  PImage img;
  Button back;

  //CONSTRUCTOR=========================
  public Shop(Player p) {
    selllist = new ArrayList<Button>();
    buylist2 = new ArrayList<Button>();
    buyer = p;
    img = loadImage("shopbackground.png");
    background(img); 
    int i = 100;
    img = loadImage("buttonpic.png");
    image(img, 5, 5, img.width/2, img.height/2);
    fill(0);
    text("Back", 12, 22);
    back = new Button(5, 5, img.width/2, img.height/2);
    ArmorList();
    img = loadImage("buttonpic.png");
    fill(190);
    //buy------------------------------------------------------
    int count = 0;
    for (InvSlot a : buylist) {
      if (count>=7) {
        i-=50;
        fill(0);
        image(img, 210, i);
        buylist2.add(new Button(210, i, 100, 50, a));
        textSize(8.4);
        text("($"+this.priceCalc(a)+")"+((Armor)(a.getPiece())).name, 215, i+25);
        textSize(12);
        count++;
      }
      else {
        fill(0);
        image(img, 80, i);
        buylist2.add(new Button(100, i, 80, 50, a));
        textSize(8.4);
        text("($"+this.priceCalc(a)+")"+((Armor)(a.getPiece())).name, 90, i+25);
        textSize(8.4);
        i+=50;
        count++;
      }
    }
    //sell-----------------------------------------------------
    for (InvSlot a : buyer.inventory) {
      if (a.getPiece() != null && !(((Armor)(a.getPiece())).name.equals(""))) {
        img = loadImage("buttonpicblue.png");
        image(img, 510, i);
        selllist.add(new Button(510, i, 100, 50, a));
        textSize(10.5);
        text(((Armor)(a.getPiece())).name, 530, i+30);
        i = i + 50;
      }
    }
    //buy
    moneyTracker();
  }

  public void ArmorList() {
    int i = 0;
    sc = new Scanner(createReader("BeginningItems.txt"));
    sc.useDelimiter(",");
    while (sc.hasNext ()) {
      if (sc.hasNextInt()) {
        r.add(sc.nextInt());
      }
      else { 
        r.add(sc.next());
      }
      i++;
    }
    int numInv=14;
    int j=-1;
    int tracker=0;

    while (tracker < numInv) {
      if ((Integer)r.get(j+1) == 1) {
        buylist.add(new InvSlot(new Weapon((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6), (Integer)r.get(j+7), (Integer)r.get(j+8))));
        j = j + 8;
      }
      else {
        if ((Integer)r.get(j+1) == 2) {
          buylist.add(new InvSlot(new Helm((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
          j = j + 6;
        }
        else {
          if ((Integer)r.get(j+1) == 3) {
            buylist.add(new InvSlot(new Body((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
            j = j + 6;
          }
          else {
            if ((Integer)r.get(j+1) == 4) {
              buylist.add(new InvSlot(new Gloves((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
              j = j + 6;
            }
            else {
              if ((Integer)r.get(j+1) == 5) {
                buylist.add(new InvSlot(new Shoes((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
                j = j + 6;
              }
              else {
                if ((Integer)r.get(j+1) == 6) {
                  buylist.add(new InvSlot(new Pants((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
                  j = j + 6;
                }
                else {
                  if ((Integer)r.get(j+1) == 7) {
                    buylist.add(new InvSlot(new Accessory((String)r.get(j+2), (Integer)r.get(j+3), (Integer)r.get(j+4), (Integer)r.get(j+5), (Integer)r.get(j+6))));
                    j = j + 6;
                  }
                }
              }
            }
          }
        }
      }
      tracker++;
    }
  } 

  public void mouseAct(int x, int y) {
    if (back.isClicked(x, y)) {
      backtomap = true;
    }
    for (Button b : buylist2) {
      if (b.isClicked(x, y)) {
        buy(b.armor);
        refresh = true;
      }
    }
    for (Button b : selllist) {
      if (b.isClicked(x, y)) {
        sell(b.armor);
      }
    }
    moneyTracker();
  }

  private double priceCalc(InvSlot i) {
    Armor a = (Armor)(i.getPiece());
    double sum= 0.0;
    sum += a.getVit() + a.getStn() + a.getIns() + a.getSpd();
    return (Math.pow(sum, 2) + 25.0);
  }

  public Character buy(InvSlot i) {
    if (buyer.inventory.size() > buyer.bagSize) {
      print("Your inventory is full.");
      return null;
    }
    Armor a = (Armor)(i.getPiece());
    int sum =0;
    sum += a.getVit() + a.getStn() + a.getIns() + a.getSpd();
    if (sum <= 3) {
      if (buyer.myMoney >= (2*sum)) {
        buyer.myMoney -= 2*sum;
        buyer.inventory.add(new InvSlot(a));
        println("Item Bought, You have $"+buyer.myMoney+" left");
        return buyer;
      }
      println("You need more money for this item.");
      return buyer;
    }
    if (sum < 8) {
      if (buyer.myMoney >= Math.pow(2, sum)) {
        buyer.myMoney -= Math.pow(2, sum);
        buyer.inventory.add(new InvSlot(a));
        println("Item Bought, You have $"+buyer.myMoney+" left");
        return buyer;
      }
      println("You need more money for this item.");
      return null;
    }
    if (buyer.myMoney >= (Math.pow(sum, 2) + 25)) {
      buyer.myMoney -= (Math.pow(sum, 2) + 25);
      buyer.inventory.add(new InvSlot(a));
      println("Item Bought, You have $"+buyer.myMoney+" left");
      return buyer;
    }
    println("You need more money for this item.");
    return buyer;
  }

  public void sell(InvSlot i) {
    Armor a = (Armor)i.getPiece();
    buyer.inventory.remove(i);
    refresh = true;
    int sum = a.getVit() + a.getStn() + a.getIns() + a.getSpd();
    if (sum <= 3) {
      buyer.myMoney += (2*sum)+20;
    }
    if (sum < 8) {
      buyer.myMoney +=(Math.pow(2, sum-3))+50;
    }
    if (sum>=15)
      buyer.myMoney += (Math.pow(sum, 2))+20;
  }

  private void moneyTracker() {
    fill(150, 120, 70);
    rect(350, 550, 80, 30);
    fill(225);
    text("$"+buyer.myMoney, 370, 570);
  }
}

